package com.creditease.rss.util;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * 异常消息工具类，功能：
 * 将异常对象转换为：字符串
 * <p/>
 * Description:
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2011-09-06    lirong       1.0        1.0 Version
 *
 * @author lirong
 * @version 1.0
 */

public class ExceptionUtil {

	/**
	 * 构造方法
	 */
    private ExceptionUtil() {
    }

    /**
     * 将异常堆栈消息转换成字符串
     * @param t Throwable
     * @return 异常信息
     */
    public static String exception2String(Throwable t) {

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        t.printStackTrace(pw);
        pw.flush();
        sw.flush();
        return sw.toString();
    }
     /**
     * 将异常堆栈消息转换成字符串
     * @param t Throwable
     * @return 异常信息
     */
    public static String exceptionStack2String(Throwable t) {

        return exception2String(t);
    }
     /**
     * 将异常堆栈消息转换成字符串
     * @param t Throwable
     * @return 异常信息
     */
    public static String exceptionChainToString(Throwable t) {

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        while (t != null) {
            t.printStackTrace(pw);
            t = t.getCause();
        }
        pw.flush();
        sw.flush();
        return sw.toString();
    }

}